﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form2 : Form      
    {
        int _type = 0;
        public Form2(ConTactPerson conTactPerson,int type)
        {
            _type = type;
            InitializeComponent();
            if (conTactPerson != null)
            {
                textId.Text = conTactPerson.id.ToString();
                textName.Text = conTactPerson.name;
                textPhone.Text = conTactPerson.phone;
                textAddress.Text = conTactPerson.address;
            }

            if (type == 1)
            {
                btnUpdate.Text = "添加";
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (_type == 1)
            {
                ConTactPerson conTactPerson = new ConTactPerson();
                conTactPerson.name = textName.Text;
                conTactPerson.phone = textPhone.Text;
                conTactPerson.address = textAddress.Text;
                string sqlStr = "insert into contact_person (name,phone,address) values(@Name,@Phone,@Address)";
                //string sqlStr = "select * from contact_person";

                MySqlParameter[] para = new MySqlParameter[]
                {
                    new MySqlParameter("@Name",conTactPerson.name),
                    new MySqlParameter("@Phone", conTactPerson.phone),
                    new MySqlParameter("@Address",conTactPerson.address)
                };
                if(MysqlHelper.ExeSql(sqlStr, para))
                {
                    MessageBox.Show("添加成功");
                }
                else
                {
                    MessageBox.Show("添加失败");
                }
            }
            else
            {
                string sqlStr = "update contact_person set name=@name,phone=@phone,address=@address where id=@id";
                MySqlParameter[] para = new MySqlParameter[]
               {
                    new MySqlParameter("@id",textId.Text),
                    new MySqlParameter("@name",textName.Text),
                    new MySqlParameter("@phone", textPhone.Text),
                    new MySqlParameter("@address",textAddress.Text)

               };
                if (MysqlHelper.ExeSql(sqlStr, para))
                {
                    MessageBox.Show("修改成功");
                }
                else
                {
                    MessageBox.Show("修改失败");
                }
            }

            this.DialogResult = DialogResult.OK;
           
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void textId_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
